<?php

// 20090211
// first implementation
//	todo : access rights!

//include(status.php);

	$ISTEXT=false;
	error_reporting (0); 

	if (isset($_REQUEST['size'])) {
		$imgsize = escapestr($_REQUEST['size']);
	} else {
		$imgsize = 0;
	}

  $imgid   = escapestr($_REQUEST['id']);

 if ($imgid==0) {
  	$filename = "images/sheep.png";
  	include ("logo.php");	
  	die();
	}
 	
	$query   = "select * from photos where id=$imgid" ;
	$result  = mysql_query($query);
	 
	if ($imgdata = mysql_fetch_array($result)) {

 		$ownerid  = $imgdata['owner'];
		$query2   = "select * from `users` where `id`='$ownerid'" ;
		$result2  = mysql_query($query2);
    $user     = mysql_fetch_array($result2);
    $owner    = $user['name']; 
//		$owner    = mysql_result($result2,0);
		$filename = "users/$owner/photos/".$imgdata['time'];
		$imginfo  = getimagesize($filename);
    $status = $user['status'];


    if ($status & $STA_BANNED) {
      if ($imgsize == 0) $imgsize = 300;
    	header('Content-type: image/png');	
		$fontsize = 5;
		if ($imgsize < 400) $fontsize = 4;
		if ($imgsize < 300) $fontsize = 3;
		if ($imgsize < 200) $fontsize = 2;
		if ($imgsize < 100) $fontsize = 1;
		if ($imgsize < 400) 
			$msg = GetLangString($lang,"txt_banned");
		else
			$msg = GetLangString($lang,"msg_banned");
			
    	$image = imagecreatetruecolor( $imgsize, $imgsize ) ;
    	$bgcolor = imagecolorallocate 	( $image,32,32,32) ; 
    	$txtcolor = imagecolorallocate ( $image,255,0,0) ; 
    	imagestring ( $image,$fontsize,5,5,$msg,$txtcolor ) ;
    	imagepng($image);
    	imagedestroy($image);
      die();
    }

    $access  = $imgdata['access'];
	$external  = $imgdata['external'];
	if ( $external && !$id)  $imgid = 0;
    //if ( ($access && !$id)  ) $imgid = 0;
	if ($id && $external == 0 && $access == 1) { // friends
		
		$query        = "SELECT * from friends where user1='$id' and user2='$ownerid'";
		$result = mysql_query($query);
					
		if (mysql_num_rows($result)){ 
			$query        = "SELECT * from friends where user2='$id' and user1='$ownerid'";
			$result = mysql_query($query);
			if (!(mysql_num_rows($result))) $imgid = 0;
		}
	}
	if ($id && $external == 0 && $access == 2) { // private
		$query        = "SELECT * from `privatephotos` where `user`='$ownerid' and `photo`='$imgid'";	
		$result = mysql_query($query);
		if (($ownerid!=$id) && (mysql_num_rows($result)==0)) $imgid = 0;
	}
	

	  if ($imgid==0) {
      if ($imgsize == 0) $imgsize = 300;
    	header('Content-type: image/png');	
    	$image = imagecreatetruecolor( $imgsize, $imgsize ) ;
    	$bgcolor = imagecolorallocate 	( $image,32,32,32) ; 
    	$txtcolor = imagecolorallocate ( $image,255,0,0) ; 
		$fontsize = 5;
		if ($imgsize < 400) $fontsize = 4;
		if ($imgsize < 300) $fontsize = 3;
		if ($imgsize < 200) $fontsize = 2;
		if ($imgsize < 100) $fontsize = 1;
		if ($id) {
			if ($access == 1 )
				imagestring ( $image,$fontsize,5,5,GetLangString($lang,"msg_photofriendsonly"),$txtcolor ) ;
			else	
				imagestring ( $image,$fontsize,5,5,GetLangString($lang,"msg_photoisprivate"),$txtcolor ) ;
			
		} else
			imagestring ( $image,$fontsize,5,5,GetLangString($lang,"msg_photomustlogin"),$txtcolor ) ;
    	imagepng($image);
    	imagedestroy($image);
      die();
  	}
	
	if ($imgsize > 150 ) {
		//Only store stats when size is over 150, so we don't count profilepics etc in the stats
	
		//http://www.gidforums.com/t-497.html
		$refdomain = preg_replace("/http:\/\//i", "", escapestr($_SERVER['HTTP_REFERER']));
		$refdomain = preg_replace("/https:\/\//i","", $refdomain);
		//$refdomain = preg_replace("/^www\./i", "", $refdomain);
		//$refdomain = preg_replace("/\/.*/i", "", $refdomain);
		$refdomain = preg_replace("/\/.*/i", "", $refdomain);
	
		$query  = "select * from `photostats` where `photo` = '$imgid' and `referer` = '$refdomain'";
		$result = mysql_query($query);
		if (mysql_num_rows($result)) {	
			$query = "UPDATE photostats set count=count+1 where photo = $imgid and `referer` = '$refdomain'";
			$result = mysql_query($query);
	
		} else {
			$query = "insert into photostats (`photo`,`count`,`referer`) values ('$imgid','1','$refdomain')";
			$result = mysql_query($query);
		}
	}

		if (
	         ($imgsize ==  50)  ||
		     ($imgsize ==  75)  ||
			 ($imgsize == 100)  ||
			 ($imgsize == 125)  ||
			 ($imgsize == 150)  ||
			 ($imgsize == 300)  ||
			 ($imgsize == 320)  ||
			 ($imgsize == 400)  ||
			 ($imgsize == 450)			 
       ) {
      if ($_REQUEST['square'] == 1) $try_cache=$filename."_".$imgsize."_1.png";
      else $try_cache=$filename."_".$imgsize."_0.png";
  		if (file_exists($try_cache)){
  		 $cached=true;
  		 header("Location: ".$try_cache);
  		 die();
			} 
		} 



		if ($imginfo[0] < $imginfo[1]) $orientation = 0 ; else $orientation = 1 ; 
		$oh = $imginfo[1]; 
		$ow = $imginfo[0];

			$filesize = filesize($filename);
			$file = fopen($filename, "rb");
			$image = fread($file,$filesize);
			fclose ($file);




		if ($imgsize) {
			if ($orientation) {
				$nw = $imgsize;
				$nh = ($nw / $ow ) * $oh ;
			} else {
				$nh = $imgsize;
				$nw = ($nh / $oh ) * $ow ;
			}



			if ($_REQUEST['square']) {
				if ($orientation) {
					$offh = ($nw - $nh) /2; 
					$offw = 0;
					
				} else {
					$offw = ($nh - $nw) /2;
					$offh = 0;
					
				}
			
				$target = imagecreatetruecolor($imgsize,$imgsize);
				$color = imagecolorallocate($target,0,0,254); 
				imagefill($target,1,1,$color);
				imagecolortransparent($target,$color);

			} else {
				$offc = 0;
				$offh = 0;
				$target = imagecreatetruecolor($nw,$nh);
			}
			$source = imagecreatefromstring($image);
			imagecopyresized($target,$source,$offw,$offh,0,0,$nw,$nh,$ow,$oh);
			header ("Content-type: image/png");
			imagepng($target);
			if ($try_cache)	imagepng($target,$try_cache);
			imagedestroy($target);
			imagedestroy($source);
		} else {
		header ("Content-type: {$imginfo['mime']}");
		echo $image;		
		}
	} else {
	// db error 
  if ($imgsize == 0) $imgsize = 300;
	header('Content-type: image/png');	
	$fontsize = 5;
	if ($imgsize < 400) $fontsize = 4;
	if ($imgsize < 300) $fontsize = 3;
	if ($imgsize < 200) $fontsize = 2;
	if ($imgsize < 100) $fontsize = 1;
	$image = imagecreatetruecolor( $imgsize, $imgsize ) ;
	$bgcolor = imagecolorallocate 	( $image,32,32,32) ; 
 	$txtcolor = imagecolorallocate ( $image,255,0,0) ; 
 	imagestring ( $image,$fontsize,5,5,GetLangString($lang,"msg_photonotfound"),$txtcolor ) ;
 	imagepng($image);
 	imagedestroy($image);
	}
	
	


?>
